package com.calculate;

import android.util.Log;

import java.util.Arrays;
import java.util.Vector;

/**
 * Created by tjpu on 2016/10/1.
 */
public class TagPos {
    public static Vector<double[]> lastPos = new Vector<>();
    private static int length = 5;

    public static double variance(double[] pos) {
        double [] tagPos = pos.clone();
        double var = 0.0;
        double [] average = {0.0, 0.0};
        double [] sum = {0.0, 0.0};

        lastPos.add(tagPos);
        if(lastPos.size() < length)
            return var;
        if(lastPos.size() > length)
            lastPos.remove(0);

        for(int i=0; i<length; i++) {
            sum[0] += tagPos[0];
            sum[1] += tagPos[1];
        }
        average[0] = sum[0]/length;
        average[1] = sum[1]/length;

        double disSum = 0.0;
        double disAve = 0.0;
        double []distance = new double[length];
        for(int i=0; i<length; i++) {
            distance[i] =  pointDistance(lastPos.get(i),average);
            disSum += distance[i];
            Log.i("tag test","tag: "+ Arrays.toString(lastPos.get(i)));
        }
        disAve = disSum/length;
        Log.i("tag test","disAve: "+ disAve);

        double varSum = 0.0;
        for(int i=0; i<length; i++){
            varSum += (distance[i] - disAve) * (distance[i] - disAve);
        }
        var += Math.sqrt(varSum)/length;
        Log.i("tag test","var: "+ var);

        return var;
    }

    private static double pointDistance(double[] point1,double[] point2) {
        return Math.sqrt((point1[0] - point2[0])*(point1[0] - point2[0])
                + (point1[1] - point2[1])*(point1[1] - point2[1]));
    }
}
